#include "../process.h"
#include "../util.h"

#define BATCH 200000

int main(int argc, const char* argv[])
{
	fstream log(argv[1], fstream::in);
	fstream out(argv[2], fstream::out);
	int count = 0;
	cout << "start\n";
	map<string, vector<string>> rank;
	map<string, vector<string>>::iterator rank_itr;
	while(!log.eof())
	{
		string line;
		getline(log, line);
		if(count ++ == 0)continue;
		if(count%BATCH == 0)cout << ".";
		int del1 = line.find('\t'), del2 = line.find('\t', del1 + 1);
		string query = line.substr(del1 + 1, del2 - del1 - 1);
		rank_itr = rank.find(query);
		if(rank_itr == rank.end())
		{
			vector<string> v;
			v.push_back(line);
			rank.insert(pair<string, vector<string>>(query, v));
		}
		else
			(*rank_itr).second.push_back(line);
	}
	for(rank_itr = rank.begin(); rank_itr != rank.end(); ++ rank_itr)
	{
		vector<string> &v = (*rank_itr).second;
		for(int i = 0; i < v.size(); ++ i)
			out << v[i] << endl;
	}
	cout << endl <<count << "\tlines processed!\n";
	log.close();
	out.close();
	return 0;
}
